package com.pingao.mp.modular.sys.controller;


import com.pingao.mp.modular.sys.entity.Menu;
import com.pingao.mp.modular.sys.entity.User;
import com.pingao.mp.modular.sys.service.MenuService;
import com.pingao.mp.utils.Constant;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 菜单表  前端控制器
 * </p>
 *
 * @author 闻品高
 * @since 2018-09-26
 */
@Controller
@RequestMapping("/sys/menu")
public class MenuController {

    /**
     * 获取登录用户对应的菜单
     */
    @Autowired
    private MenuService menuService;

    @GetMapping("/getMenu")
    @RequiresUser
    @ResponseBody
    public Object getMenu() {
        List<Menu> list = menuService.getLoginMenu();
        Map<String, Object> map = new HashMap<>();
        map.put("rows", list);
        return map;
    }

    @GetMapping("/index")
    @RequiresPermissions("sys.menu.index")
    public String index() {
        return "sys/menu/index";
    }

    @GetMapping("/list")
    @RequiresPermissions("sys.menu.index")
    @ResponseBody
    public Object list(@RequestParam(value = "search", required = false) String search) {
        Map<String, Object> map = new HashMap<>();
        map.put("rows", menuService.getList(search));
        return map;
    }

    @GetMapping("/add")
    @RequiresPermissions("sys.menu.add")
    public String add() {
        return "sys/menu/add";
    }

    @PostMapping("/add")
    @RequiresPermissions("sys.menu.add")
    @ResponseBody
    public Object add(Menu menu) {
        Map<String, Object> map = new HashMap<>();
        User u = (User) SecurityUtils.getSubject().getSession().getAttribute(Constant.LoginUser);
        menu.setUserId(u.getId());
        menu.setCreateTime(LocalDateTime.now());
        menu.setUpdateTime(LocalDateTime.now());
        if (!menuService.save(menu)) {
            map.put("status", false);
            map.put("msg", "保存菜单出错......");
            return map;
        }
        map.put("status", true);
        map.put("msg", "保存菜单成功");
        map.put("menu", menu);
        return map;
    }

    @GetMapping("/ztree")
    @RequiresPermissions("sys.menu.add")
    @ResponseBody
    public Object ztree(@RequestParam(value = "id", required = false) Integer id) {
        return menuService.getMenuTree(id);
    }

    @PostMapping("/delete")
    @RequiresPermissions("sys.menu.delete")
    @ResponseBody
    public Object delete(
            @RequestParam(value = "ids[]") List<Integer> ids
    ) {
        Map<String, Object> map = new HashMap<>();
        for (Integer id : ids) {
            menuService.deleteMenuById(id);
        }
        map.put("status", true);
        map.put("msg", "删除成功");
        map.put("ids", ids);
        return map;
    }


    @GetMapping("/edit/{id}")
    @RequiresPermissions("sys.menu.edit")
    public String edit(@PathVariable("id") Integer id, ModelMap map) {
        Menu menu = menuService.getById(id);
        if (menu == null) {
            throw new NullPointerException("该菜单不存在--this menu is not exits!!!");
        }
        Menu parentMenu = menuService.getById(menu.getParentId());
        map.addAttribute("menu", menu);
        map.addAttribute("parentMenuName", parentMenu == null ? "" : parentMenu.getName());
        return "sys/menu/edit";
    }

    @PostMapping("/edit")
    @RequiresPermissions("sys.menu.edit")
    @ResponseBody
    public Object edit(Menu menu) {
        Map<String, Object> map = new HashMap<>();
        User u = (User) SecurityUtils.getSubject().getSession().getAttribute(Constant.LoginUser);
        menu.setUserId(u.getId());
        menu.setUpdateTime(LocalDateTime.now());
        if (!menuService.updateById(menu)) {
            map.put("status", false);
            map.put("msg", "保存菜单出错......");
            return map;
        }
        map.put("status", true);
        map.put("msg", "保存菜单成功");
        map.put("menu", menu);
        return map;
    }
}